package com.yeyks.controller.hotel;

import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.em.ResponseStatus;
import com.yeyks.hotel.dal.dao.CompanyInfoMapper;
import com.yeyks.hotel.dal.domain.CompanyInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Beacon
 * @version $Id: CompanyController.java, v 0.1 2018年08月09日 11:21 Beacon Exp $
 */

@Api(tags = "公司模块")
@RestController
public class CompanyController {

    @Autowired
    CompanyInfoMapper companyInfoMapper;


    /**
     * @param param
     * @return
     */
    @RequestMapping(value = "/ajax/company/update", method = RequestMethod.POST)
    public ResponseEntity<?> update(
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            CompanyInfo param
    ) {
        AjaxResponseBody response = new AjaxResponseBody();

        try {

            if (param.getId() != null) {
                companyInfoMapper.updateByPrimaryKey(param);
            } else if (param.getBizName() != null && !param.getBizName().isEmpty()) {
                companyInfoMapper.updateByBizName(param);
            } else if (param.getBizNo() != null && param.getBizNo().isEmpty()) {

            }

            response.setCode(ResponseStatus.RS_SUCCESS.getCode());
            response.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (DuplicateKeyException e) {
            response.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            response.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            response.setCode(ResponseStatus.RS_FAIL.getCode());
            response.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(response);
    }

    /**
     * @param token
     * @param id
     * @param key
     * @return
     */
    @RequestMapping(value = "/ajax/company/search", method = RequestMethod.POST)
    public ResponseEntity<?> search(
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "id", defaultValue = "0", required = true) Integer id,
            @RequestParam(name = "key", defaultValue = "", required = true) String key
    ) {
        AjaxResponseBody response = new AjaxResponseBody();

        try {

            List<CompanyInfo> result = companyInfoMapper.selectByAny(id, key);

            response.setBody(result);

            response.setCode(ResponseStatus.RS_SUCCESS.getCode());
            response.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (DuplicateKeyException e) {
            response.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            response.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            response.setCode(ResponseStatus.RS_FAIL.getCode());
            response.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(response);
    }

    /**
     * @param token
     * @param id
     * @param key
     * @return
     */
    @RequestMapping(value = "/ajax/company/del", method = RequestMethod.POST)
    public ResponseEntity<?> del(
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "id", defaultValue = "0", required = true) Integer id,
            @RequestParam(name = "key", defaultValue = "", required = true) String key
    ) {
        AjaxResponseBody response = new AjaxResponseBody();

        try {

            int deleted = companyInfoMapper.deleteByAny(id, key);

            response.setBody(deleted);

            response.setCode(ResponseStatus.RS_SUCCESS.getCode());
            response.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (DuplicateKeyException e) {
            response.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            response.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            response.setCode(ResponseStatus.RS_FAIL.getCode());
            response.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(response);
    }
}